package com.dcrzl.shcz.module.sys.controller;

import com.dcrzl.shcz.common.entity.sys.SysMenu;
import com.dcrzl.shcz.common.utils.BaseController;
import com.dcrzl.shcz.common.utils.PageInfo;
import com.dcrzl.shcz.common.utils.Result;
import com.dcrzl.shcz.module.sys.service.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 菜单管理
 *
 * @author dcrzl
 * @email 1728795786@qq.com
 * @date 2022-05-09 10:49:26
 */
@RestController
@RequestMapping("/sys/menu")
@Slf4j
public class SysMenuController extends BaseController {
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result<List<SysMenu>> list(SysMenu sysMenu){
       return sysMenuService.selectList(sysMenu);
    }

    /**
    * 列表分页
    */
    @GetMapping("/page")
    public PageInfo<SysMenu> page(SysMenu sysMenu){
        return sysMenuService.selectPage(sysMenu);
    }

    /**
     * 信息
     */
    @GetMapping("/info")
    public Result<SysMenu> info(@RequestParam Long id){
		return sysMenuService.selectById(id);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody SysMenu sysMenu){
		return sysMenuService.insert(sysMenu);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result<Integer> update(@RequestBody SysMenu sysMenu){
		return sysMenuService.updateById(sysMenu);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public Result<Integer> delete(@RequestBody Long[] ids){
        return sysMenuService.deleteBatchIds(Arrays.asList(ids));
    }

    @GetMapping("/nav")
    public Result<List<SysMenu>> nav() {
        Result Result =
                sysMenuService.getUserMenuList(this.getRoleId());
        List<SysMenu> list = (List<SysMenu>) Result.getData();
        //根据orderNum进行从小到大排序
        list.sort(Comparator.comparingInt(SysMenu::getOrderNum));
        //权限列表
        List<String> permissionsList = new ArrayList<>();
        list.forEach(sysMenu -> {
            if (sysMenu != null && sysMenu.getPerms() != null) {
                permissionsList.add(sysMenu.getPerms());
            }
        });
        Set<String> permissionSet = new HashSet<>();
        permissionsList.forEach(permissions->{
            permissionSet.addAll(Arrays.asList(permissions.split(",")));
        });
        //封装返回结果集
        Map<String, Object> map = new HashMap<>();
        //菜单列表
        map.put("menuList", sysMenuService.getSubMenu(list).getData());
        map.put("permissions", permissionSet);
        log.info(map.toString());
        return new Result().setData(map).setSuccess(true);
    }

    @GetMapping("/list/role")
    public Result<List<SysMenu>> selectMenuListByRoId() {
        Result Result =
                sysMenuService.getUserMenuList(this.getRoleId());
        List<SysMenu> menuList = (List<SysMenu>) Result.getData();
        HashMap<Long, SysMenu> menuMap = new HashMap<>();
        for (SysMenu s : menuList) {
            menuMap.put(s.getId(), s);
        }
        for (SysMenu s : menuList) {
            SysMenu parent = menuMap.get(s.getParentId());
            if (Objects.nonNull(parent)) {
                s.setParentName(parent.getName());
            }
        }
        return Result;
    }



}
